package ArrayList;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentCrm {
    public static void main(String[] args) {
//        添加,有判断重复.  添加成功回到最外层.
//                删除根据学号, 是唯一的. 不存在的,查无其人, 回到外层. 成功回到外层
//        修改 根据学号, 不存在, 查无此人.
//        5退出
        ArrayList<StdCrm> array = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        startCrm(sc, array);

    }

    private static void startCrm(Scanner sc, ArrayList<StdCrm> array) {
        System.out.println("已进入学生管理系统");
        int flag = 0;
        while (flag != 5){
            System.out.println("1.新增学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.查看学生信息");
            System.out.println("5.退出");

            int flag2=   sc.nextInt();
            if(flag2>=1 && flag2 <=5){

                if (flag2 == 1){
                        addArr(sc,array);
                }
                else if(flag2 == 2){
                    delArr(sc,array);
                }
                else if(flag2 == 3){
                    setArr(sc,array);
                }
                else if(flag2 == 4){
                    getArr(array);
                }
                else {
                    System.out.println("欢迎下次使用");
                    flag = 5;
                }
            }else {
                System.out.println("输入错误,请重新输入");
            }
        }
    }

    public  static void addArr(Scanner sc,ArrayList<StdCrm> arrayList){
        StdCrm stduent = new StdCrm();
        for (int i = 0; i < 3; i++) {
            if(i == 0){
                System.out.println("请输入学号-int");
                int s = sc.nextInt();
                if(isRepeat(arrayList,s)){
                    System.out.println("当前学生学号已重复");
                };
                stduent.setStuNumber(s);
            }else if(i == 1){
                System.out.println("请输入姓名");
                String name = sc.next();
                stduent.setName(name);
            }else if(i == 2){
                System.out.println("请输入年龄");
                int age = sc.nextInt();
                stduent.setAge(age);
            }
        }
        arrayList.add(stduent);
    }
    public static void  delArr(Scanner sc,ArrayList<StdCrm> arrayList){
        int index = sc.nextInt();
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getStuNumber() == index){
                StdCrm s =    arrayList.remove(i);
                System.out.println("被删除的信息:"+s);
                break;
            }
            System.out.println(arrayList.get(i));
        }
    }
    public static void  setArr(Scanner sc,ArrayList<StdCrm> arrayList){
        int selectIndex = -1;
        StdCrm newStduent = new StdCrm();
        for (int i = 0; i < 3; i++) {
            if(i == 0){
                System.out.println("请输入学号-int-set");
                int s = sc.nextInt();
                selectIndex = getOne(arrayList,s);
                if(selectIndex == -1) {
                    System.out.println("不存在当前信息");
                    i--;
                }else{
                    newStduent.setStuNumber(s);
                }
            }else if(i == 1){
                System.out.println("请输入姓名-set");
                String name = sc.next();
                newStduent.setName(name);

            }else if(i == 2){
                System.out.println("请输入年龄-set");
                int age = sc.nextInt();
                newStduent.setAge(age);
            }
        }
        arrayList.set(selectIndex,newStduent);
    }
    public static void  getArr(ArrayList<StdCrm> arrayList){
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
    }
    public  static boolean isRepeat(ArrayList<StdCrm> arrayList,int StuNumber){
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getStuNumber() == StuNumber)return  true;
        }
        return  false;
    }

    public static int getOne(ArrayList<StdCrm> arrayList,int StuNumber){
        for (int i = 0; i < arrayList.size(); i++) {
           if( arrayList.get(i).getStuNumber() == StuNumber){
               return i;
           }
        }
        return -1;
    }
}




































